package com.hxd.otc.base;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.hxd.otc.R;
import com.hxd.otc.mvp.contract.BaseRecyclerContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * Created by lzy on 2018/6/7 0007.
 * description ${TODO}
 * <p>
 * Changed by somebody on 2018/6/7 0007
 * description ${TODO}
 */
public abstract class BaseRecyclerActivity<P extends BaseRecyclerPresenter, A extends BaseAdapter, T> extends BaseActivity<P>
        implements BaseRecyclerContract.View<A, T>, OnItemClickListener,
        OnItemLongClickListerner, OnLoadMoreListener, OnRefreshListener {

    protected RecyclerView mRecyclerView;
    protected SmartRefreshLayout mRefreshLayout;
    protected A mAdapter;
    private View mEmptyView;
    private View mErrorView;
    private View mTvRefresh;
    protected ViewGroup mFlRecyclerContainer;

    @Override
    protected int getLayoutId() {
        return R.layout.layout_recycler;
    }

    @Override
    protected void initView() {
        mRecyclerView = findViewById(R.id.recyclerView);
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mFlRecyclerContainer = findViewById(R.id.fl_recycler_container);

        if (mRecyclerView != null) {
            mRecyclerView.setLayoutManager(getLayoutManager());
            mAdapter = createAdapter();
            mAdapter.setOnItemClickListener(this);
            mAdapter.setOnItemLongClickListerner(this);
            mRecyclerView.setAdapter(mAdapter);
            RecyclerView.ItemDecoration decor = getDividerItemDecoration();
            if (decor != null) mRecyclerView.addItemDecoration(decor);
        }

        if (mRefreshLayout != null) {
            mRefreshLayout.setOnLoadMoreListener(this);
            mRefreshLayout.setOnRefreshListener(this);
            mRefreshLayout.setEnableRefresh(isEnableRefresh());
            mRefreshLayout.setEnableLoadMore(isEnableLoadMore());

            if (isAutoRefresh()) {
                if (isEnableRefresh()) {
                    mRefreshLayout.autoRefresh(0, 10, 1);
                } else {
                    onRefresh(mRefreshLayout);
                }
            }
        }
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getApplicationContext());
    }

    protected abstract A createAdapter();

    protected RecyclerView.ItemDecoration getDividerItemDecoration() {
        return null;
    }

    @Override
    public boolean isEnableLoadMore() {
        return true;
    }

    @Override
    public boolean isEnableRefresh() {
        return true;
    }

    @Override
    public boolean isAutoRefresh() {
        return true;
    }

    @Override
    public void setEnableLoadMore(boolean enable) {
        if (mRefreshLayout != null) mRefreshLayout.setEnableLoadMore(enable);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPresenter.onRefresh();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mPresenter.onLoadMore();
    }


    @Override
    public void onLoadDataSuccess(boolean isRefresh, List<T> data) {
        mAdapter.addAll(isRefresh, data);
        onFinishLoad();
        setErrorViewVisibility(View.GONE);
        if (data != null && data.size() > 0) {
            setEmptyViewVisibility(View.GONE);
        } else {
            setEmptyViewVisibility(isRefresh ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void onLoadDataFail(boolean isRefresh, String msg) {
        showToast(msg);
        onFinishLoad();

        if (isRefresh) {
            setErrorViewVisibility(View.VISIBLE);
        } else {
            setErrorViewVisibility(View.GONE);
            setEmptyViewVisibility(View.GONE);
        }
    }

    public void setErrorViewVisibility(int visibility) {
        boolean isGone = visibility == View.GONE;
        if (isGone) {
            if (mErrorView == null) return;
            mErrorView.setVisibility(View.GONE);
        } else {
            if (getRecyclerContainer() == null) return;
            if (mErrorView == null) {
                mErrorView = getLayoutInflater().inflate(R.layout.error_view, null);
                mFlRecyclerContainer.addView(mErrorView);
                mTvRefresh = mErrorView.findViewById(R.id.tv_refresh);
                if (mTvRefresh != null) {
                    mTvRefresh.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mPresenter.onRefresh();
                        }
                    });
                }
            } else {
                mErrorView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setEmptyViewVisibility(int visibility) {
        boolean isGone = visibility == View.GONE;
        if (isGone) {
            if (mEmptyView == null) return;
            mEmptyView.setVisibility(View.GONE);
        } else {
            if (getRecyclerContainer() == null) return;
            if (mEmptyView == null) {
                mEmptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
                getRecyclerContainer().addView(mEmptyView);
            } else {
                mEmptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    protected ViewGroup getRecyclerContainer() {
        return mFlRecyclerContainer;
    }


    @Override
    public void onFinishLoad() {
        if (mRefreshLayout == null) return;
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    @Override
    public void onItemClick(int position) {
    }

    @Override
    public boolean onItemLongClick(int position) {
        return false;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {

    }
}
